<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//CORREGIR VARIABLE DE SESION EN EL CONTROLLER AGREGAR Y EDITAR ELIMINAR
//BOTON MAS PONER LINK
class AdminPeliculas extends CI_Controller {

	public function index(){
		$data['session_user'] = $this->session->userdata('session_user');
		$data["mensaje"] = NULL;
		$data['titulo']= 'Peliculas';
		$data['accion']="alert-success";
       
		//~ mostrar las peliculas
		if($data['session_user']!=NULL){
			//~ $this->load->view('admin/admin_peliculas.php',$data);//HAY QUE CAMBIARLA Y HACER UNA LISTA DE TODOS LOS ARCHIVOS
			$this->ListarPelicula($data);
		}
		else {
			$data['session_user'] = NULL;
			$data['mensaje']="Debes Iniciar Sesion";
			$this->load->view('admin/login.php',$data);
		}

	}
	
	public function agregar(){
		
		$data['session_user'] = $this->session->userdata('session_user');
		$data['titulo']= 'Peliculas';
		$data['accion']="Agregar";
		$data['pelicula']=NULL;
		$this->load->view('admin/form_pelicula.php',$data);
	
	}
	
	public function agregarPelicula(){
	
		$titulo = $this->input->post("titulo");
		$anyo = $this->input->post("anyo");
		$sinopsis = $this->input->post("sinopsis");
		$genero = $this->input->post("genero");
		if($genero == "0")
		{
			$genero = "SinGenero";
		}
		$data['session_user'] = $this->session->userdata('session_user');
		$data['titulo']= 'Peliculas';
		$data['accion']="alert-error";
		
		if($_FILES["posterName"]["name"]!=""){
			
			if($_FILES["videoName"]["name"]!=""){
				
				$data['accion']="alert-success";
				$data['mensaje'] = "Insertado Exitosamente";
				
				$info = $this->do_upload($this);
				
				$rutaPoster = $info[0]["file_name"];//$_FILES["posterName"]["name"];
				$nameVideo = $info[1]["file_name"];//$_FILES["videoName"]["name"];
				
				$ruta = "/var/www/hmservi/peliculas/";
				
				$rutaVideo = substr($nameVideo,0,strpos($nameVideo,strrchr($nameVideo,"."))).".flv";
				//~ $rutaVideo = strrchr($nameVideo,".").".flv";
				
				$this->load->model("pelicula_modelo");
				$r = $this->pelicula_modelo->InsertarPelicula($titulo,$anyo,$genero,$sinopsis,$rutaPoster,$rutaVideo);
				
				
				echo shell_exec("ffmpeg -i ".$ruta.$nameVideo." -sameq -ar 44100 ".$ruta.$rutaVideo." >/dev/null 2>/dev/null &");
				
				//~ echo exec("rm -rf ".$ruta.$nameVideo);

			}
			else
			{
				$data['mensaje'] = "Error al subir el archivo de Video";
			}
			
		}
		else
		{
			$data['mensaje'] = "Error al subir el archivo Poster";
		}
		
		//~ $this->load->view("admin/admin_peliculas",$data);
		$this->ListarPelicula($data);
	
	}

	public function editar($id){
		$data['session_user'] = $this->session->userdata('session_user');

		$data['titulo']="Peliculas";
		$data["accion"] = "Editar";
		$data["titulo"] = "Peliculas";
		
		$this->load->model("pelicula_modelo");
		$data["pelicula"] = $this->pelicula_modelo->ObtenerPelicula($id);
		
		$this->load->view('admin/form_pelicula.php',$data);
	
	}
	
	public function editarPelicula()
	{
		$id = $this->input->post("id");
		$titulo = $this->input->post("titulo");
		$anyo = $this->input->post("anyo");
		$sinopsis = $this->input->post("sinopsis");
		$genero = $this->input->post("genero");
		$data['session_user'] = $this->session->userdata('session_user');
		$data['titulo']= 'Peliculas';
		$data['accion']="alert-error";

		$this->load->model("pelicula_modelo");
		
		$data['accion']="alert-success";
		$data['mensaje'] = "Modificado Exitosamente";
		
		$this->ListarPelicula($data);
		$this->pelicula_modelo->EditarPelicula($id,$titulo, $anyo, $genero, $sinopsis);
	}

	public function do_uploadPoster($objeto)
	{
		$config['upload_path'] = '/var/www/hmservi/poster/';
		$config['allowed_types'] = 'gif|jpg|txt|png';
		//~ $config['max_size']	= '100';
		//~ $config['max_width']  = '1024';
		//~ $config['max_height']  = '768';

		$objeto->load->library('upload', $config);

		if ( ! $objeto->upload->do_upload('posterName'))
		{
			$error= $objeto->upload->display_errors();

			//~ $this->load->view('upload_form', $error);
			echo $error;
			
		}
		else
		{
			$r = array('upload_data' => $objeto->upload->data());

			//~ $this->load->view('upload_success', $data);
			//~ return $r['orig_name'];
			
			
		}
	}
	public function do_uploadVideo($objeto)	{
		$config['upload_path'] = '/var/www/hmservi/peliculas/';
		$config['allowed_types'] = 'mp4|avi|mpg|txt';
		//~ $config['max_size']	= '100';
		//~ $config['max_width']  = '1024';
		//~ $config['max_height']  = '768';

		$objeto->load->library('upload', $config);

		if ( ! $objeto->upload->do_upload('videoName'))
		{
			$error= $objeto->upload->display_errors();

			//~ $this->load->view('upload_form', $error);
			echo $error;
			
		}
		else
		{
			$r = array('upload_data' => $objeto->upload->data());

			//~ $this->load->view('upload_success', $data);
			//~ return $r['orig_name'];
			
			
		}
	}
	
	public function ListarPelicula($data=NULL){
		$datos['session_user'] = $this->session->userdata('session_user');
		if(!is_numeric($data))
		{
			$datos["mensaje"] = $data["mensaje"];
			$datos["titulo"] = $data['titulo'];
			$datos["accion"] = $data['accion'];
		}
		else
		{
			$datos["mensaje"] = "";
			$datos["titulo"] = "Peliculas";
			$datos["accion"] = "";

		}
		
		$this->load->helper('url');
		$this->load->model('pelicula_modelo');
		if($this->uri->segment(3))
            {
                $pagina=$this->uri->segment(3);
            }else
            {
               $pagina=0;
            }
		if($datos["accion"]=="Eliminar")
		{
			$pagina=0; $datos["accion"]="alert-success"; 
		}
        $datos['result']=$this->pelicula_modelo->ListarPelicula($pagina,"limit");
		$datos['cuantos']=$this->pelicula_modelo->ListarPelicula($pagina,"cuantos");
		$config['base_url'] = base_url().'index.php/adminPeliculas/ListarPelicula/';
		$config['total_rows'] = $datos['cuantos'];
		$config['per_page'] = 10;
		$config['uri_segment'] = '3';
		$config['num_links'] = '8';
		
		$this->pagination->initialize($config);
		$this->load->view('admin/admin_peliculas',$datos);
	}
	
	public function do_upload($objeto)
	{
            $objeto->load->library('upload');
 
            $config['upload_path'] = '/var/www/hmservi/poster/';
            $config['allowed_types'] = '*';
            $config['max_size']	= '1500000';
            $objeto->upload->initialize($config);
 
			$i = 0;
            foreach($_FILES as $field => $file)
            {
				//~ echo 'aqui';
                // No problems with the file
                if($file['error'] == 0)
                {
					//~ echo $field;
					//~ echo $file['name'];
                    // So lets upload
                    if ($objeto->upload->do_upload($field))
                    {
                        $r[$i] = $objeto->upload->data();
                        
                    }
                    else
                    {
                        $errors = $objeto->upload->display_errors();
                        echo $errors;
                    }
                }
                $config['upload_path'] = '/var/www/hmservi/peliculas/';
				$config['allowed_types'] = '*';
				$config['max_size']	= '1500000';
				$objeto->upload->initialize($config);
				$i++;
            }
			return $r;

	}
	
	public function eliminar($id){

		$data['session_user'] = $this->session->userdata('session_user');

		$data['titulo']="Peliculas";
		$data['accion']="Eliminar";

		$data['mensaje']="Su Pelicula ha sido eliminado exitosamente...";


		
		
		
		$this->load->model('pelicula_modelo');
		
		
		
		$pelicula = $this->pelicula_modelo->ObtenerPelicula($id);

		
		
		$this->pelicula_modelo->EliminarPelicula($id);

		
	
		echo exec("rm -rf /var/www/hmservi/peliculas/".$pelicula[0]->rutaVideo);
		echo exec("rm -rf /var/www/hmservi/poster/".$pelicula[0]->rutaPoster);

		$this->ListarPelicula($data);
	}

}


?>
